package cake.web.servlet;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cake.dao.impl.AddressDaoImpl;
import cake.dao.impl.CakeDaoImpl;
import cake.dao.impl.CakeOrderDaoImpl;
import cake.dao.impl.DetailsDaoImpl;
import cake.dao.impl.UserDaoImpl;
import cake.entity.Address;
import cake.entity.Cake;
import cake.entity.CakeOrder;
import cake.entity.Details;

import cake.entity.User;
@WebServlet("/handonorder.html")
public class HandOnOrderServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
    	List<Cake> cakes = new ArrayList<Cake>();
    	CakeDaoImpl cakeDaoImpl = new CakeDaoImpl();
    	AddressDaoImpl addressDaoImpl = new AddressDaoImpl();
    	UserDaoImpl userDaoImpl = new UserDaoImpl();  	
    	User LoginUser = (User) request.getSession().getAttribute("LoginUser");
    	if(LoginUser==null) {
    		response.sendRedirect("login.html");
    		return;
    	}
    	request.getSession().setAttribute("LoginUser",LoginUser);
    	List<Address> addresses=addressDaoImpl.findAddressByUid(LoginUser.getId());
    	request.setAttribute("addresses",addresses);
    	String[] cids;
    	if(request.getParameterValues("cid")==null || request.getParameterValues("cid").length==0) {
    		response.sendRedirect("cart.html");
    		return;
    	}else {
    		 cids=request.getParameterValues("cid");
    	        for(int i=0;i<cids.length;i++) {
    	        	int cid=Integer.parseInt(cids[i]);
    	        	Cake cake=cakeDaoImpl.getCakeByCidAndUid(cid,LoginUser.getId());
    	        	cakes.add(cake);
    	        }
    	        request.setAttribute("cakes",cakes);
    	}
        float totalPrice=Float.parseFloat(request.getParameter("totalPrice"));
        request.setAttribute("totalPrice",totalPrice);
        request.getRequestDispatcher("handonorder.jsp").forward(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
    	DetailsDaoImpl detailsDaoImpl = new DetailsDaoImpl();
    	CakeDaoImpl cakeDaoImpl = new CakeDaoImpl();
    	CakeOrderDaoImpl cakeOrderDaoImpl = new CakeOrderDaoImpl();
    	
    	String[] cids=request.getParameterValues("cid");
    	String[] counts=request.getParameterValues("count");
    	String[] unitprices=request.getParameterValues("unitprice");
    	User user = (User) request.getSession().getAttribute("LoginUser");
    	Calendar calendar =Calendar.getInstance();
    	int oid=Integer.parseInt(user.getId()+""+calendar.get(calendar.DATE)+calendar.get(calendar.MINUTE)+calendar.get(calendar.SECOND));
    	int aid=Integer.parseInt(request.getParameter("addressid"));
    	AddressDaoImpl addressDaoImpl = new AddressDaoImpl();
    	Address adres=addressDaoImpl.getAddressById(aid);
    	Float totalPrice =Float.parseFloat(request.getParameter("totalPrice"));
    	CakeOrder cakeOrder = new CakeOrder(0,user.getId(),
    			adres.getAddress(),adres.getConsignee(),totalPrice, 
    			oid,new Date(new java.util.Date().getTime()),"未发货");
    	cakeOrderDaoImpl.addOrder(cakeOrder);
    	int detailsid=cakeOrderDaoImpl.getOrderByOid(oid);
    	for(int i=0;i<cids.length;i++) {
    		int cid=Integer.parseInt(cids[i]);
    		int count=Integer.parseInt(counts[i]);
    		cakeDaoImpl.updateStock(cid, count);
    		float unitprice=Float.parseFloat(unitprices[i]);
    		Details details = new Details(cid, count,detailsid, unitprice);
    		detailsDaoImpl.addDetails(details);
    	}	
    	response.sendRedirect("user.html");
    }
}
